import { createParagraph, getParagraphs, delParagraph, editParagraph,getAllParagraphs } from '../service';
import { message } from 'antd';

const creator = (type, payload) => ({ type, payload });
export default {
  namespace: 'paragraph',
  state: {
    paragraphs: [],
    content:[]
  },

  effects: {
    *createParagraph({ payload }, { call, put }) {
      let res = yield call(createParagraph, payload);
      if (res.httpStatus === 200) {
        message.success(`${res.msg}`);
      } else {
        message.warn(`${res.msg}`);
      }
      return res;
    },
    // 获取段落内容不处理
    *getParagraphs({payload}, { call, put }) {
      let res = yield call(getAllParagraphs);
      if (res.httpStatus === 200) {
        yield put(creator('save', { paragraphs: res.data }));
      } else {
        message.warn(`${res.msg}`);
      }
    },
      // 获取段落内容需要处理
    *getContent({payload}, { call, put }) {
      let res = yield call(getParagraphs,payload);
      if (res.httpStatus === 200) {
        let content=res.data.sort((a,b)=>a.paragraph-b.paragraph)
        yield put(creator('save', { content: res.data }));
      } else {
        message.warn(`${res.msg}`);
      }
    },
    *delParagraphs({ payload }, { call, put }) {
      let res = yield call(delParagraph, payload);
      return res;
    },
    *editParagraphs({ payload }, { call, put }) {
      let res = yield call(editParagraph, payload);
      return res;
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  //监听路由
  // subscriptions: {
  //   setup({ dispatch, history }) {
  //     return history.listen(({ pathname, query }) => {
  //       if (pathname === '/users') {
  //         console.log(query,9999)
  //         dispatch({ type: 'fetch', payload: query });
  //       }
  //     })
  //   }
  // }
};
